package ms07.ticket.sms;

import ms07.ticket.DebugLog;
import ms07.ticket.MyApplication;
import ms07.ticket.R;
import ms07.ticket.Ticket;
import ms07.ticket.TicketRequest;
import ms07.ticket.activities.ActivityTicketRequestReceived;
import ms07.ticket.activities.ActivityTickets;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.TextView;

public class Receiver extends BroadcastReceiver {
	protected String[] tempMessage; 
	
	public Receiver(){
		this.tempMessage = new String[2];
		
		this.tempMessage[0] = "ÖBB TICKET Nr:26159098\n"+
			"C1: WGJFD2\n"+
			"C2: 7ASCIP\n"+
			"C3: WFN6N6\n"+
			"micsch\n"+
			"Salzburg\n"+
			"Wien Wes\n"+
			"Gültig am\n"+
			"14.05.10\n"+
			"1 Erw.\n"+
			"VORTEILScard\n"+
			"0 Kind(er)\n"+
			"2. Kl.\n"+
			"EUR 23.8";
		this.tempMessage[1] = "ÖBB ANBOT\n"+
			"Wien Wes\n"+
			"Salzburg\n"+
			"Gültig 11.07.10\n"+
			"1 Erw\n"+
			"2. Kl\n"+
			"micsch\n"+
			"Kaufen: Antworte \"V\" für Vorteilscard (EUR 23.8), \"N\" für Normalpreis (EUR 47.5)";
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
        Bundle bundle = intent.getExtras();
        
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
        	smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        
        this.handleMessage(context, smsMessage[0].getMessageBody(), smsMessage[0].getOriginatingAddress());
        //this.handleMessage(context, tempMessage[1], smsMessage[0].getOriginatingAddress());
	}
	
	protected void handleMessage(Context context, String message, String number){

        DebugLog.getInstance().log("Received SMS: " + message);
        
        Parser parser = Parser.getInstance();
        
        if(!parser.checkNumber(number)){
        	DebugLog.getInstance().log("SMS from unknown number ["+number+"] -> ignoring it!");
        	return;
        }else{
        	DebugLog.getInstance().log("SMS from VALID number ["+number+"]");
        }
        
        parser.setText(message);
        
        try{
        	parser.parseText();
        	
        	Ticket ticket = parser.getTicket();
        	
        	if(ticket instanceof TicketRequest){
        		//damn, just a request
        		DebugLog.getInstance().log("Received Offer");
        		
        		//display nice chooser wether to by with/without membercard
        		//Tickets tickets = Tickets.getInstance();
        		//Intent myIntent = new Intent(context, TicketRequestReceived.class);
				//this.startActivityForResult(myIntent,0);
				
				Intent activityIntent = new Intent();
				
				activityIntent.setClass(context, ActivityTicketRequestReceived.class);
				//activityIntent.putExtras(intent.getExtras());
				activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				activityIntent.putExtra("ticket", ticket);
				context.startActivity(activityIntent);
				
        		//Dialog dialog = new Dialog(MyApplication.getInstance());

				//dialog.setContentView(R.layout.received_request);
				//dialog.setTitle("Custom Dialog");

				//TextView text = (TextView) dialog.findViewById(R.id.text);
				//text.setText("Hello, this is a custom dialog!");
				
        		//dialog.show();
        		//Tickets tickets = Tickets.getInstance();
        		//tickets.set //TODO somehow give the right object!!!!
        		//tickets.showDialog(Tickets.DIALOG_TICKET_REQUEST_ID);
        	}else{
        		//cool we got a real ticket!
        		DebugLog.getInstance().log("Received Ticket");
        		ticket.save();
            	
            	if(ActivityTickets.getInstance()!=null){
            		ActivityTickets.getInstance().addTicket(ticket);
            	}
        	}
        }catch(Exception e){
        	//TODO handle exception
        	DebugLog.getInstance().error("ERROR: "+e.getMessage(), e);
        }
	}

}
